SystemConfiguration = new Object();

var configuration_store = Ext.create('Ext.data.Store', {
	fields : [ 'companyName', 'companyAddress', 'nameOfBank', 'accountNo', 'mapja', 'withholdingTax', 'minAllowIssuerRate', 'server', 'serverPassword', 'outgoinAddress' ]
});

var emailStore = Ext.create('Ext.data.Store', {
	fields : [ 'email' ]
});

var rating_store = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'code', 'creditRating', 'ratingValue' ]
});
SystemConfiguration.getCenterPanel = function() {

	return formPanel = Ext.create('Ext.form.FormPanel', {
		title : 'System Configuration',
		bodyPadding : '20',
		autoScroll : true,
		frame : true,
		id : 'sys_conf_form',
		defaults : {
			labelWidth : 175,
			anchor : '65%',
		},
		fieldDefaults : {
			labelAlign : 'left',
			labelWidth : 150,
		},
		items : [ {
			xtype : 'textfield',
			fieldLabel : 'Company Name:',
			name : 'companyName'
		}, {
			xtype : 'textarea',
			fieldLabel : 'Company Address',
			name : 'companyAddress'
		}, {
			xtype : 'textfield',
			fieldLabel : 'Name of Bank',
			name : 'nameOfBank'
		}, {
			xtype : 'textfield',
			fieldLabel : 'Account Number',
			name : 'accountNo'
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			fieldLabel : 'Compliance recipients E-mail',
			items : [ {
				xtype : 'textfield',
				vtype : 'email',
				flex : 1,
				padding : '0 5 0 0',
				listeners : {
					specialkey : function(field, e) {
						if (e.getKey() == e.ENTER) {
							if (field.isValid()) {
								var recordIndex = emailStore.findBy(function(record, id) {
									if (record.get('email') === field.getValue()) {
										return true;
									}
									return false;
								});

								if (recordIndex != -1) {
									Ext.Msg.alert('Error', "Duplicate recode fund");
								} else {
									emailStore.add({
										email : field.getValue()
									});
									field.setRawValue('');
								}
							} else {
								Ext.Msg.alert('Error', "Invalid format");
							}
						}
					}
				},
			}, {
				xtype : 'grid',
				width : 250,
				store : emailStore,
				height : 100,
				id : 'email_grid',
				columns : [ {
					text : 'E-mail',
					dataIndex : 'email',
					flex : 1
				}, {
					xtype : 'actioncolumn',
					width : 50,
					align : 'center',
					items : [ {
						icon : 'images/Delete.jpg',
						tooltip : 'Delete',
						handler : function(grid, rowIndex, colIndex) {
							var rec = grid.getStore().getAt(rowIndex);
							if (rec) {
								emailStore.remove(rec);
							}
						}
					} ]
				} ],
				listeners : {
					selectionchange : function(sm, selectedRecord) {
						if (selectedRecord.length) {
							// var detailPanel = Ext.getCmp('detailPanel');
							console.log(this.body, selectedRecord[0].data);
						}
					}
				}
			} ]
		}, {
			xtype : 'numberfield',
			fieldLabel : 'Maximum Applicant per in a Joint Account',
			name : 'mapja',
			keyNavEnabled : false,
			mouseWheelEnabled : false,
			minValue : 0,
			maxValue : 10,
		}, {
			xtype : 'numberfield',
			fieldLabel : 'Withholding Tax',
			name : 'withholdingTax',
			keyNavEnabled : false,
			mouseWheelEnabled : false,
			minValue : 0,
		}, {
			xtype : 'combo',
			fieldLabel : 'Minimum Allowed Issuer Rating',
			queryMode : 'local',
			store : rating_store,
			valueField : 'id',
			displayField : 'creditRating',
			flex : 1,
			id : 'minAllowIssuerRate',
			name : 'minAllowIssuerRate',
			emptyText : 'Select a Credit Rating...',
		}, {
			xtype : 'fieldcontainer',
			layout : 'vbox',
			fieldLabel : 'E-mail Configuration',
			items : [ {
				xtype : 'textfield',
				fieldLabel : 'Server',
				// regex : '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$',
				// regexText : 'sdas',
				name : 'server'
			}, {
				xtype : 'textfield',
				fieldLabel : 'Outgoing Address',
				vtype : 'email',
				name : 'outgoinAddress'
			}, {
				xtype : 'textfield',
				fieldLabel : 'Password',
				inputType : 'password',
				name : 'serverPassword'
			} ]
		} ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
			}
		}, {
			text : 'Save',
			id : 'save',
			handler : function() {
				var form = this.up('form').getForm();
				if (form.isValid()) {
					var email_grids = Ext.getCmp('email_grid');
					var emails = null;
					email_grids.getStore().each(function(record) {
						if (emails == null) {
							emails = record.get('email');
						} else {
							emails = emails + "," + record.get('email');
						}
					});
					formPanel.setLoading('Saving...');
					form.url = 'addSystemConfiguration.action';
					form.submit({
						params : {
							"compemails" : Ext.encode(emails)
						},
						success : function(form, action) {
							form.reset();
							Ext.getCmp('save').setText('Save');
							Ext.Msg.alert('Success', 'Saved Successfully');
							emailStore.removeAll();

							Ext.Ajax.request({
								url : 'getSystemConfiguration.action',
								success : function(response) {
									var formPanl = Ext.getCmp('sys_conf_form');
									var obj = Ext.decode(response.responseText);
									configuration_store.loadRawData(obj.configurationDtos);
									configuration_store.each(function(record) {
										formPanl.getForm().loadRecord(record);
										Ext.getCmp('minAllowIssuerRate').setValue(parseInt(record.raw.minAllowIssuerRate));
									});

									emailStore.loadRawData(obj.emails);

								}
							});
							formPanel.setLoading(false);
						},
						failure : function(form, action) {
							Ext.Msg.alert('Failed', 'There is some error returned from the server.');
							formPanel.setLoading(false);
						}
					});

				}
			}
		} ],
		listeners : {
			render : function(p) {
				Ext.Ajax.request({
					url : 'getAllCreditRates.action',
					success : function(response) {
						var obj = Ext.decode(response.responseText);
						rating_store.loadRawData(obj.creditRatingDtos);
					}
				});
				Ext.Ajax.request({
					url : 'getSystemConfiguration.action',
					success : function(response) {
						var formPanl = Ext.getCmp('sys_conf_form');
						var obj = Ext.decode(response.responseText);
						configuration_store.loadRawData(obj.configurationDtos);
						configuration_store.each(function(record) {
							formPanl.getForm().loadRecord(record);
							Ext.getCmp('minAllowIssuerRate').setValue(parseInt(record.raw.minAllowIssuerRate));
						});

						emailStore.loadRawData(obj.emails);

					}
				});
			}
		}
	});
};
